// cloudfunctions/searchProducts/index.js
const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database()

exports.main = async (event, context) => {
  const { query } = event
  try {
    const res = await db.collection('products')
      .where({
        name: db.RegExp({
          regexp: query,
          options: 'i', // 不区分大小写
        })
      })
      .get()
    return {
      success: true,
      data: res.data
    }
  } catch (err) {
    return {
      success: false,
      error: err
    }
  }
}
